3 * Cache for outputs of the PHP parser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Cache Parser
25 * @ingroup Cache Parser
32 * Get an instance of this object
36 public static function singleton() {
38 if ( !isset( $instance ) ) {
40 $instance = new ParserCache( $parserMemc );
46 * Setup a cache pathway with a given back-end storage mechanism.
48 * This class use an invalidation strategy that is compatible with
49 * MultiWriteBagOStuff in async replication mode.
51 * @param BagOStuff $memCached
54 protected function __construct( BagOStuff
$memCached ) {
55 $this->mMemc
= $memCached;
59 * @param WikiPage $article
61 * @return mixed|string
63 protected function getParserOutputKey( $article, $hash ) {
66 // idhash seem to mean 'page id' + 'rendering hash' (r3710)
67 $pageid = $article->getId();
68 $renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
70 $key = $this->mMemc
->makeKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
75 * @param WikiPage $page
76 * @return mixed|string
78 protected function getOptionsKey( $page ) {
79 return $this->mMemc
->makeKey( 'pcache', 'idoptions', $page->getId() );
83 * @param WikiPage $page
86 public function deleteOptionsKey( $page ) {
87 $this->mMemc
->delete( $this->getOptionsKey( $page ) );
91 * Provides an E-Tag suitable for the whole page. Note that $article
92 * is just the main wikitext. The E-Tag has to be unique to the whole
93 * page, even if the article itself is the same, so it uses the
94 * complete set of user options. We don't want to use the preference
95 * of a different user on a message just because it wasn't used in
96 * $article. For example give a Chinese interface to a user with
97 * English preferences. That's why we take into account *all* user
98 * options. (r70809 CR)
100 * @param WikiPage $article
101 * @param ParserOptions $popts
104 public function getETag( $article, $popts ) {
105 return 'W/"' . $this->getParserOutputKey( $article,
106 $popts->optionsHash( ParserOptions
::legacyOptions(), $article->getTitle() ) ) .
107 "--" . $article->getTouched() . '"';
111 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
112 * @param WikiPage $article
113 * @param ParserOptions $popts
114 * @return ParserOutput|bool False on failure
116 public function getDirty( $article, $popts ) {
117 $value = $this->get( $article, $popts, true );
118 return is_object( $value ) ?
$value : false;
122 * Generates a key for caching the given article considering
123 * the given parser options.
125 * @note Which parser options influence the cache key
126 * is controlled via ParserOutput::recordOption() or
127 * ParserOptions::addExtraKey().
129 * @note Used by Article to provide a unique id for the PoolCounter.
130 * It would be preferable to have this code in get()
131 * instead of having Article looking in our internals.
133 * @todo Document parameter $useOutdated
135 * @param WikiPage $article
136 * @param ParserOptions $popts
137 * @param bool $useOutdated (default true)
138 * @return bool|mixed|string
140 public function getKey( $article, $popts, $useOutdated = true ) {
142 return $this->getKeyReal( $article, $popts, $useOutdated, $dummy );
146 * Temporary internal function to allow accessing $usedOptions
147 * @todo Merge this back to self::getKey() when ParserOptions::optionsHashPre30() is removed
148 * @param WikiPage $article
149 * @param ParserOptions $popts
150 * @param bool $useOutdated (default true)
151 * @param array &$usedOptions Don't use this, it will go away soon
152 * @return bool|mixed|string
154 private function getKeyReal( $article, $popts, $useOutdated, &$usedOptions ) {
155 global $wgCacheEpoch;
157 if ( $popts instanceof User
) {
158 wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
159 $popts = ParserOptions
::newFromUser( $popts );
162 // Determine the options which affect this article
164 $optionsKey = $this->mMemc
->get(
165 $this->getOptionsKey( $article ), $casToken, BagOStuff
::READ_VERIFIED
);
166 if ( $optionsKey instanceof CacheTime
) {
167 if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
168 wfIncrStats( "pcache.miss.expired" );
169 $cacheTime = $optionsKey->getCacheTime();
170 wfDebugLog( "ParserCache",
171 "Parser options key expired, touched " . $article->getTouched()
172 . ", epoch $wgCacheEpoch, cached $cacheTime\n" );
174 } elseif ( !$useOutdated && $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
175 wfIncrStats( "pcache.miss.revid" );
176 $revId = $article->getLatest();
177 $cachedRevId = $optionsKey->getCacheRevisionId();
178 wfDebugLog( "ParserCache",
179 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
184 // $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
185 $usedOptions = $optionsKey->mUsedOptions
;
186 wfDebug( "Parser cache options found.\n" );
188 if ( !$useOutdated ) {
191 $usedOptions = ParserOptions
::legacyOptions();
194 return $this->getParserOutputKey(
196 $popts->optionsHash( $usedOptions, $article->getTitle() )
201 * Retrieve the ParserOutput from ParserCache.
202 * false if not found or outdated.
204 * @param WikiPage|Article $article
205 * @param ParserOptions $popts
206 * @param bool $useOutdated (default false)
208 * @return ParserOutput|bool False on failure
210 public function get( $article, $popts, $useOutdated = false ) {
211 global $wgCacheEpoch;
213 $canCache = $article->checkTouched();
215 // It's a redirect now
219 $touched = $article->getTouched();
222 $parserOutputKey = $this->getKeyReal( $article, $popts, $useOutdated, $usedOptions );
223 if ( $parserOutputKey === false ) {
224 wfIncrStats( 'pcache.miss.absent' );
229 /** @var ParserOutput $value */
230 $value = $this->mMemc
->get( $parserOutputKey, $casToken, BagOStuff
::READ_VERIFIED
);
232 $parserOutputKey = $this->getParserOutputKey(
234 $popts->optionsHashPre30( $usedOptions, $article->getTitle() )
236 $value = $this->mMemc
->get( $parserOutputKey, $casToken, BagOStuff
::READ_VERIFIED
);
239 wfDebug( "ParserOutput cache miss.\n" );
240 wfIncrStats( "pcache.miss.absent" );
244 wfDebug( "ParserOutput cache found.\n" );
246 // The edit section preference may not be the appropiate one in
247 // the ParserOutput, as we are not storing it in the parsercache
248 // key. Force it here. See T33445.
249 $value->setEditSectionTokens( $popts->getEditSection() );
251 $wikiPage = method_exists( $article, 'getPage' )
252 ?
$article->getPage()
255 if ( !$useOutdated && $value->expired( $touched ) ) {
256 wfIncrStats( "pcache.miss.expired" );
257 $cacheTime = $value->getCacheTime();
258 wfDebugLog( "ParserCache",
259 "ParserOutput key expired, touched $touched, "
260 . "epoch $wgCacheEpoch, cached $cacheTime\n" );
262 } elseif ( !$useOutdated && $value->isDifferentRevision( $article->getLatest() ) ) {
263 wfIncrStats( "pcache.miss.revid" );
264 $revId = $article->getLatest();
265 $cachedRevId = $value->getCacheRevisionId();
266 wfDebugLog( "ParserCache",
267 "ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
271 Hooks
::run( 'RejectParserCacheValue', [ $value, $wikiPage, $popts ] ) === false
273 wfIncrStats( 'pcache.miss.rejected' );
274 wfDebugLog( "ParserCache",
275 "ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
279 wfIncrStats( "pcache.hit" );
286 * @param ParserOutput $parserOutput
287 * @param WikiPage $page
288 * @param ParserOptions $popts
289 * @param string $cacheTime Time when the cache was generated
290 * @param int $revId Revision ID that was parsed
292 public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
293 $expire = $parserOutput->getCacheExpiry();
294 if ( $expire > 0 && !$this->mMemc
instanceof EmptyBagOStuff
) {
295 $cacheTime = $cacheTime ?
: wfTimestampNow();
297 $revision = $page->getRevision();
298 $revId = $revision ?
$revision->getId() : null;
301 $optionsKey = new CacheTime
;
302 $optionsKey->mUsedOptions
= $parserOutput->getUsedOptions();
303 $optionsKey->updateCacheExpiry( $expire );
305 $optionsKey->setCacheTime( $cacheTime );
306 $parserOutput->setCacheTime( $cacheTime );
307 $optionsKey->setCacheRevisionId( $revId );
308 $parserOutput->setCacheRevisionId( $revId );
310 $parserOutputKey = $this->getParserOutputKey( $page,
311 $popts->optionsHash( $optionsKey->mUsedOptions
, $page->getTitle() ) );
313 // Save the timestamp so that we don't have to load the revision row on view
314 $parserOutput->setTimestamp( $page->getTimestamp() );
316 $msg = "Saved in parser cache with key $parserOutputKey" .
317 " and timestamp $cacheTime" .
318 " and revision id $revId" .
321 $parserOutput->mText
.= "\n<!-- $msg -->\n";
324 // Save the parser output
325 $this->mMemc
->set( $parserOutputKey, $parserOutput, $expire );
327 // ...and its pointer
328 $this->mMemc
->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
330 // Normally, when there was no key change, the above would have
331 // overwritten the old entry. Delete that old entry to save disk
333 $oldParserOutputKey = $this->getParserOutputKey( $page,
334 $popts->optionsHashPre30( $optionsKey->mUsedOptions
, $page->getTitle() ) );
335 $this->mMemc
->delete( $oldParserOutputKey );
338 'ParserCacheSaveComplete',
339 [ $this, $parserOutput, $page->getTitle(), $popts, $revId ]
341 } elseif ( $expire <= 0 ) {
342 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );